package com.god.gl.ble.adapter;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothHeadset;
import android.bluetooth.BluetoothHidDevice;
import android.bluetooth.BluetoothProfile;
import android.content.Context;
import android.os.Build;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.clj.fastble.data.BleDevice;
import com.god.gl.ble.R;
import com.god.gl.ble.utils.HidConncetUtil;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.RequiresApi;

/**
 * Created by liushen on 2016/5/31.
 */
public class MyBluetoothAdapter extends BaseAdapter {
    private List<BluetoothDevice> mList = new ArrayList<>();
    private Context mContext;
    private HidConncetUtil mHidConncetUtil;
    private BluetoothHeadset mBluetoothHeadset;
    private BluetoothHidDevice mBluetoothHidDevice;

    public MyBluetoothAdapter(List<BluetoothDevice> mList, Context mContext) {
        this.mList = mList;
        this.mContext = mContext;
        //4.0以上才支持HID模式
        if (Build.VERSION.SDK_INT >= 14) {
            this.mHidConncetUtil = new HidConncetUtil(mContext);
        }
    }

    public MyBluetoothAdapter(Context mContext) {
        this.mContext = mContext;
        //4.0以上才支持HID模式
        if (Build.VERSION.SDK_INT >= 14) {
            this.mHidConncetUtil = new HidConncetUtil(mContext);
        }
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public void addData(BluetoothDevice bluetoothDevice) {
        mList.add(bluetoothDevice);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = View.inflate(mContext, R.layout.blue_list_item, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        initData(position, viewHolder);
        return convertView;
    }

    private void initData(int position, ViewHolder viewHolder) {
        final BluetoothDevice bluetoothDevice = mList.get(position);
        viewHolder.bluehandlename.setText(bluetoothDevice.getName());
        isBonded(bluetoothDevice, viewHolder);
        isConnected(bluetoothDevice, viewHolder);
        viewHolder.connect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mHidConncetUtil != null) {
                    //连接
                      mHidConncetUtil.connect(bluetoothDevice);
                  /*  BleDevice bleDevice = new BleDevice(bluetoothDevice);
                       connect(bleDevice);*/

                }
            }
        });

        viewHolder.bonded.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //配对
                mHidConncetUtil.pair(bluetoothDevice);
            }
        });
    }

    /**
     * 判断是否配对并设置配置状态
     *
     * @param bluetoothDevice
     */
    private void isBonded(BluetoothDevice bluetoothDevice, ViewHolder viewHolder) {
        int state = bluetoothDevice.getBondState();
        switch (state) {
            case BluetoothDevice.BOND_NONE:
                viewHolder.bluehandlebond.setText("未配对");
                break;
            case BluetoothDevice.BOND_BONDING:
                viewHolder.bluehandlebond.setText("配对中...");
                break;
            case BluetoothDevice.BOND_BONDED:
                viewHolder.bluehandlebond.setText("已配对");
                break;
        }
    }

    /**
     * 判断是否连接
     *
     * @param bluetoothDevice
     * @param viewHolder
     */
    private void isConnected(final BluetoothDevice bluetoothDevice, final ViewHolder viewHolder) {
        if (mHidConncetUtil != null) {
            mHidConncetUtil.getHidConncetList(new HidConncetUtil.GetHidConncetListListener() {
                @Override
                public void getSuccess(ArrayList<BluetoothDevice> list) {
                    //判断连接列表中是否有该设备
                    for (BluetoothDevice bluetoothDevice1 : list) {
                        if (bluetoothDevice.getAddress().equals(bluetoothDevice1.getAddress())) {
                            viewHolder.bluehandlebond.setText("HID已连接");
                            break;
                        }
                    }
                }
            });
        }
    }

    private void connect(BluetoothDevice bluetoothDevice) {
        if (mHidConncetUtil != null) {
            mHidConncetUtil.connect(bluetoothDevice);
        }
    }

    public class ViewHolder {
        public final TextView bluehandlename;
        public final TextView bluehandlebond;
        public final Button connect;
        public final View root;
        public final Button bonded;

        public ViewHolder(View root) {
            bluehandlename = (TextView) root.findViewById(R.id.blue_handle_name);
            bluehandlebond = (TextView) root.findViewById(R.id.blue_handle_bond);
            connect = (Button) root.findViewById(R.id.connect);
            bonded = (Button) root.findViewById(R.id.bonded);
            this.root = root;
        }
    }


    private void connect(final BleDevice bleDevice) {
        BluetoothAdapter.getDefaultAdapter().getProfileProxy(mContext, new BluetoothProfile.ServiceListener() {
            @RequiresApi(api = Build.VERSION_CODES.P)
            @Override
            public void onServiceConnected(int profile, BluetoothProfile proxy) {
                Log.e("BluetoothProfile",profile+"");
                 if (profile == BluetoothProfile.HID_DEVICE) {
                     try {
                         mBluetoothHidDevice = (BluetoothHidDevice) proxy;
                         mBluetoothHidDevice.connect(bleDevice.getDevice());
                     } catch (Exception e) {
                         e.printStackTrace();
                     }

                }
            }

            @Override
            public void onServiceDisconnected(int profile) {

            }
        }, BluetoothProfile.HID_DEVICE);





       /* BleManager.getInstance().connect(bleDevice, new BleGattCallback() {
            @Override
            public void onStartConnect() {

            }

            @Override
            public void onConnectFail(BleDevice bleDevice, BleException exception) {

            }

            @Override
            public void onConnectSuccess(BleDevice bleDevice, BluetoothGatt gatt, int status) {
                Log.e("bleDevice",bleDevice.getDevice().getAddress());
            }

            @Override
            public void onDisConnected(boolean isActiveDisConnected, BleDevice bleDevice, BluetoothGatt gatt, int status) {


            }
        });*/
    }
}
